<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="./js/vue2.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="app">
        <father></father>
    </div>
    <template id="father">
        <div class="father">
            <h1>我是父组件 叫{{fname}}</h1>
            <input type="button" value="点击告诉" @click="tellmessage">
            <son></son>
        </div>
    </template>
    <template id="son">
        <div class="son">
            <h2>我是子组件 叫{{sname}}</h2>
            <h3>今天我父亲 告诉我一个好消息 他说我是{{getmessage}}</h3>
        </div>
    </template>
    
</body>
<script>
    // 创建事件总线的实例对象
    var bus= new Vue()
    // 父组件
    Vue.component('father',{
        template:"#father",
        data(){
            return {
                fname:"林天",
                goodmessage:'富二代'
            }
        },
        methods: {
          tellmessage(){
            bus.$emit('getmessage',this.goodmessage) 
          }
        },
        components: {
            'son':{
                template:"#son",
                data(){
                    return{
                        sname:"林中鸟",
                    getmessage:"??"
                    }
                },
                mounted () {
                    bus.$on('getmessage',(data)=>{
                        this.getmessage=data;
                    })
                }
            }
        }
    })
    var vm = new Vue({
        el:"#app",
        data:{
            
        }
    })
</script>
</html>